package com.yiwei.zxp.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ResetFirstShopPhotoBean;
import com.yiwei.zxp.api.bean.UploadImgBean;
import com.yiwei.zxp.api.model.ShopAuthInfoModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.fragment.ShopFirstShopFragment;
import com.yiwei.zxp.fragment.ShopIdentityOneFragment;
import com.yiwei.zxp.util.ImageUtil;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.HomeViewPager;
import com.yiwei.zxp.view.V19FrameLayout;
import com.yiwei.zxp.view.uploadImage.UploadImgAdapter;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ShopIdentityInfoActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_license_has)
    SuperButton btnLicenseHas;
    @BindView(R.id.btn_license_without)
    SuperButton btnLicenseWithout;
    @BindView(R.id.pager)
    HomeViewPager pager;

    private Unbinder unbiner;
    private UploadImgAdapter uploadImgAdapter;
    private ShopIdentityOneFragment hasFragment;
    private ShopFirstShopFragment noFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_identity_info);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("店铺认证");
        getShopAuthInfo();
    }

    private void setFragmentData(ShopAuthInfoModel.DatasBean item) {
        hasFragment = new ShopIdentityOneFragment();
        hasFragment.setData(item);
        noFragment = new ShopFirstShopFragment();
        noFragment.setImageFile(item.shop_img);
        Fragment[] fragments = new Fragment[]{hasFragment, noFragment};
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
    }


    public void getShopAuthInfo() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        Api.api_service.getShopAuthInfo().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setFragmentData(result.datas);
                });
    }

    public void getUploadImg(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        UploadImgBean bean = new UploadImgBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.getUploadImg(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    noFragment.setImageBitmap(bitmap);
                    getUploadFirst(result.datas.url);
                });
    }


    public void getUploadFirst(String url) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ResetFirstShopPhotoBean bean = new ResetFirstShopPhotoBean();
        bean.img = url;
        Api.api_service.getResetShopFirstPhoto(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "上传成功");
                });
    }

    public void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImg(file);
                }
            }

            @Override
            public void callTake(File file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImg(ImageUtil.decodeUri(ShopIdentityInfoActivity.this, Uri.parse(file.getAbsolutePath())));
                }
            }
        }, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_license_has, R.id.btn_license_without})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_license_has:
                setPageChange(0);
                break;
            case R.id.btn_license_without:
                setPageChange(1);
                break;
        }
    }

    private void setPageChange(int poi) {
        if (poi == 0) {
            btnLicenseHas.setShapeSolidColor(0xffFF0030).setUseShape();
            btnLicenseHas.setTextColor(0xffffffff);
            btnLicenseWithout.setShapeSolidColor(0xffffffff).setUseShape();
            btnLicenseWithout.setTextColor(0xff010101);
        } else {
            btnLicenseWithout.setShapeSolidColor(0xffFF0030).setUseShape();
            btnLicenseWithout.setTextColor(0xffffffff);
            btnLicenseHas.setShapeSolidColor(0xffffffff).setUseShape();
            btnLicenseHas.setTextColor(0xff010101);

        }
        pager.setCurrentItem(poi);
    }
}
